<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>WebSocket Connect</title>
    <script src="js/sockjs-0.3.4.js" type="text/javascript"></script>
    <script src="js/stomp.js" type="text/javascript"></script>
    <script>
        var stomClient = null;

        function setConnected(connected) {
            if(connected){
                console.log("Connected ................"   )
            }
        }
        function connect () {
            var socket = new SockJS(getContextPath() + "/ws");
            stomClient = Stomp.over(socket);
            stomClient.connect({}, function (frame) {
                setConnected(true);
                console.log("Connected :" + frame);
                //stomClient.subscribe("/topic/greetings", function (greeting) {
                stomClient.subscribe("/topic/helloMsg", function (greeting) {
                    console.info(greeting.body);
                });
                
                stomClient.subscribe("/topic/hello.lixuwei", function (greeting) {
                    console.info(greeting.body);
                });

                stomClient.subscribe("/app/first", function (greeting) {
                    console.info(greeting.body);
                });


                
            });
        }

        function disconnect () {
            stomClient.disconnect(function (callback) {
                console.log("disconnected " + callback);
            })
        }

        //获取contextpath
        function getContextPath() {
            var pathName = document.location.pathname;
            var index = pathName.substr(1).indexOf("/");
            var result = pathName.substr(0,index+1);
            return result;
        }

        function sendMsg () {
            stomClient.send("/app/helloMsg", {}, "yyyyyyyyyyyyyy");
        } 
        
        function sendMsg_with_path () {
            stomClient.send("/app/hello.lixuwei", {}, "QQQQQQQQQQQQQQQQ");
        }

        connect();
        
    </script>
</head>
<body>
    <button  onclick="disconnect()">Disconnect</button>
    <button  onclick="sendMsg()">SendMsg</button>
    <button  onclick="sendMsg_with_path()">SendMsg_With_Path</button>
</body>
</html>